<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class AccountKindForm extends CFormModel {

    public $name;
    public $const_pay;
    public $start_pay;
    public $card_pay;
    public $in_pay;
    public $out_pay;
    public $max_limit;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('name,const_pay,start_pay,card_pay,in_pay,out_pay, max_limit', 'required', 'message' => 'Pole wymagane'),
        );
    }
            
    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'name' => 'Nazwa konta',
            'const_pay' => 'Opłata za prowadzenie',
            'start_pay' => 'Opłata za aktywacje',
            'card_pay' => 'Opłata za kartę',
            'in_pay' => 'Opłata przelew zewnętrzny',
            'out_pay' => 'Opłata przelew wewnętrzny',
            'max_limit' => 'Maksymalne zadłużenie',           
        );
    }

    public function checkdate($attribute, $params) {
        //if (!$this->hasErrors()) {
        $bd = $this->birthday;
        // $birthday can be UNIX_TIMESTAMP or just a string-date.
        if (is_string($bd)) {
            $bd = strtotime($bd);
        }

        // check
        // 31536000 is the number of seconds in a 365 days year.
        if (time() - $bd < 18 * 31536000) {
            //var_dump('zle');
            $this->addError('birthday', 'Trzeba mieć ukończone 18 lat');
        }
        // }
    }

    public function checkpesel($attribute, $params) {
        if (!$this->hasErrors()) {
            $this->addError('password', 'Niepoprawny pesel');
        }
    }
    
    public function addAccount(){
        $result = Yii::app()->db->createCommand("SELECT createrodzajkontafun('".$this->name."','".$this->const_pay."', '".$this->start_pay."','".$this->card_pay."', '".$this->in_pay."', '".$this->out_pay."', '".$this->max_limit."')")->queryAll();
        return $result[0]['createrodzajkontafun'];
    }


}
